package com.wuyan.locationdog.view

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.arcsoft.commonbase.bean.ShareTimeBean
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.DogTypeListAdapter
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig


class DogTypePopup(
    context: Context,
    var sureCallback: (String) -> Unit
) : BasePopupWindow(context) {

    private var inputDialog: InputDialog? = null
    private val mAdapter by lazy { DogTypeListAdapter(R.layout.item_dog_type, null) }


    init {
        contentView = createPopupById(R.layout.pop_dog_type)
        popupGravity = Gravity.BOTTOM
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        findViewById<View>(R.id.btnClose).setOnClickListener {
            dismiss()
        }
        findViewById<View>(R.id.btnSure).setOnClickListener {
            sureCallback.invoke(mAdapter.data.find { it.isSelect }?.time.toString())
            dismiss()
        }
        findViewById<RecyclerView>(R.id.recyclerView).apply {
            layoutManager = GridLayoutManager(context, 3)
            adapter = mAdapter.apply {
                val list = mutableListOf<ShareTimeBean>()
                list.add(
                    ShareTimeBean("下司犬", true)
                )
                list.add(
                    ShareTimeBean("川东猎犬", false)
                )
                list.add(
                    ShareTimeBean("虎斑犬", false)
                )
                list.add(
                    ShareTimeBean("细犬", false)
                )
                list.add(
                    ShareTimeBean("太仓猎犬", false)
                )
                list.add(
                    ShareTimeBean("青川犬", false)
                )
                list.add(
                    ShareTimeBean("箭毛猎犬", false)
                )
                list.add(
                    ShareTimeBean("比特犬", false)
                )
                list.add(
                    ShareTimeBean("罗威纳犬", false)
                )
                list.add(
                    ShareTimeBean("马犬", false)
                )
                list.add(
                    ShareTimeBean("手动添加", false)
                )
                setNewInstance(list)
                addChildClickViewIds(R.id.shapeView)
                setOnItemChildClickListener { adapter, view, position ->
                    if (data[position].time == "手动添加") {
                         inputDialog = InputDialog(ActivityUtils.getTopActivity(), title = "品种", hintText = "请输入") {
                            sureCallback.invoke(it)
                            inputDialog?.dismiss()
                            dismiss()
                        }
                        inputDialog?.show()
                        return@setOnItemChildClickListener
                    }
                    data.find { it.isSelect }?.isSelect = false
                    data[position].isSelect = true
                    notifyDataSetChanged()
                }
            }
        }
    }

    override fun onCreateShowAnimation(): Animation? {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.FROM_BOTTOM)
            .toShow()
    }

}
